plugins {
    id("com.android.application")
    id("rustPlugin")
    {{~#each android-app-plugins}}
    id("{{this}}"){{/each}}
}

android {
    compileSdk = 31
    defaultConfig {
        applicationId = "{{reverse-domain app.domain}}.{{snake-case app.name}}"
        minSdk = {{android.min-sdk-version}}
        targetSdk = 31
        versionCode = 1
        versionName = "1.0"
    }
    sourceSets.getByName("main") {
        {{#if android.vulkan-validation}}// Vulkan validation layers
        val ndkHome = System.getenv("NDK_HOME")
        jniLibs.srcDir("${ndkHome}/sources/third_party/vulkan/src/build-android/jniLibs"){{/if}}
    }
    buildTypes {
        getByName("debug") {
            isDebuggable = true
            isJniDebuggable = true
            isMinifyEnabled = false
            packagingOptions {
                {{~#each targets}}
                jniLibs.keepDebugSymbols.add("*/{{this.abi}}/*.so"){{/each}}
            }
        }
        getByName("release") {
            isMinifyEnabled = false
            proguardFiles(getDefaultProguardFile("proguard-android.txt"), "proguard-rules.pro")
        }
    }
    flavorDimensions.add("abi")
    productFlavors {
        {{~#each targets}}
        create("{{this.arch}}") {
            dimension = "abi"
            ndk {
                abiFilters += listOf("{{this.abi}}")
            }
        }{{/each}}
    }

    assetPacks += mutableSetOf({{quote-and-join-colon-prefix asset-packs}})
}

rust {
    rootDirRel = "{{root-dir-rel}}"
    targets = listOf({{quote-and-join target-names}})
    arches = listOf({{quote-and-join arches}})
}

dependencies {
    {{~#each android-app-dependencies-platform}}
    implementation(platform("{{this}}")){{/each}}
    {{~#each android-app-dependencies}}
    implementation("{{this}}"){{/each}}
}

afterEvaluate {
    android.applicationVariants.all {
        val buildType = "${buildType.name.capitalize()}"
        productFlavors.forEach {
            val archAndBuildType = name.capitalize()
            tasks["merge${archAndBuildType}JniLibFolders"].dependsOn(tasks["rustBuild${archAndBuildType}"])
        }
    }
}
